<?php

namespace Biz\model\wiki;

class DingDanXiaGoods extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'dingdanxia_goods';

    public static function addData($data)
    {
        $id = self::getInfo($data['item_id'], $data['type']);
        if ($id) {
            self::updateData($data, ['id' => $id]);
            return $id;
        } else {
            $id = self::insertData($data);
        }
        return $id;
    }

    public static function getInfo($itemId, $type, $field = 'id')
    {
        return self::get($field, [
            'type' => $type,
            'item_id' => $itemId
        ]);
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}